package cnpm02.j2ee.group3.model;

import static javax.persistence.CascadeType.MERGE;
import static javax.persistence.CascadeType.PERSIST;

import java.util.Date;

import javax.crypto.SecretKey;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Type;
import org.hibernate.validator.NotNull;

@Entity
@Table(name = "PERSON", uniqueConstraints = @UniqueConstraint(columnNames = {
                "Login"}))
@AttributeOverride(name = "id", column = @Column(name = "PersonId"))
public class Person extends AbstractEntity {

	private static final long serialVersionUID = 1L;
	
	@NotNull
	@Column(name = "Login", nullable = false, length = 50)
	private String login;
	
	@NotNull
	@Column(name = "FirstName", nullable = false, length = 50)
	private String firstname;
	
	@NotNull
	@Column(name = "LastName", nullable = false, length = 50)
	private String lastname;
	
	@NotNull
	@Column(name = "Birthday", nullable = false)
	@Temporal(TemporalType.DATE)
	private Date birthday;
	
	@NotNull
	@Enumerated(EnumType.STRING)
	@Column(name = "Gender", nullable = false, length = 6)
	private Gender gender;
	
	@NotNull
	@ManyToOne(cascade = {PERSIST, MERGE})
	@JoinColumn(name = "NationalityId", nullable = false)
	private Nationality nationality;
	
	@ManyToOne(cascade = {PERSIST, MERGE})
	@JoinColumn(name = "LanguageId", nullable =false)
	@NotNull
	private Language language;
	
	@NotNull
	@Column(name = "Password", nullable = false, length = 100)
	private String password;
	
	@Column(name = "AdvertiseAllowed", nullable = false)
	@Type(type = "true_false")
	private boolean advertiseAllowed;
	
	@Column(name = "PersonalInfoPublicAllowed", nullable = false)
	@Type(type = "true_false")
	private boolean personalInfoPublicAllowed;
	
	@ManyToOne(cascade = {PERSIST, MERGE})
	@JoinColumn(name = "SecretQuestionId", nullable = false)
	@NotNull
	private SecretQuestion secretQuestion;
	
	@Column(name = "Answer", nullable = false)
	@NotNull
	private String answer;
	
	@Type(type = "cnpm02.j2ee.group3.model.PhoneNumber")
    @Column(name = "PhoneNumber", nullable = false)
    @NotNull
	private PhoneNumber phoneNumber;
	
	@Column(name = "Email", nullable = false)
	@NotNull
	private String email;
	
	@Type(type = "javax.crypto.SecretKey")
    @Column(name = "SecretKey")
	private SecretKey key;
	
	//required for jpa
	public Person(){
		super();
	}
	
	
	public Person(String firstname, String lastname, Date birthday,
			Gender gender, Nationality nationlity, Language language,
			SecretQuestion secretQuestion, String answer,
			PhoneNumber phoneNumber, String email, String login,
			String password, boolean advertisingAllowed, boolean credentialCheck) {
		this();
		this.firstname = firstname;
		this.lastname = lastname;
		this.birthday = birthday;
		this.gender = gender;
		this.nationality = nationlity;
		this.language = language;
		this.secretQuestion = secretQuestion;
		this.advertiseAllowed = advertisingAllowed;
		this.personalInfoPublicAllowed = credentialCheck;
		this.answer = answer;
		this.phoneNumber = phoneNumber;
		this.email = email;
		this.login = login;
		this.password = password;
	}
	
	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	
	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	public Gender getGender() {
		return gender;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getLogin() {
		return login;
	}

	public void setNationality(Nationality nationality) {
		this.nationality = nationality;
	}

	public Nationality getNationality() {
		return nationality;
	}

	public void setLanguage(Language language) {
		this.language = language;
	}

	public Language getLanguage() {
		return language;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setAdvertiseAllowed(boolean advertiseAllowed) {
		this.advertiseAllowed = advertiseAllowed;
	}

	public boolean isAdvertiseAllowed() {
		return advertiseAllowed;
	}

	public void setSecretQuestion(SecretQuestion secretQuestion) {
		this.secretQuestion = secretQuestion;
	}

	public SecretQuestion getSecretQuestion() {
		return secretQuestion;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}


	public String getAnswer() {
		return answer;
	}


	public void setPhoneNumber(PhoneNumber phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public PhoneNumber getPhoneNumber() {
		return phoneNumber;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public void setPersonalInfoPublicAllowed(boolean personalInfoPublicAllowed) {
		this.personalInfoPublicAllowed = personalInfoPublicAllowed;
	}


	public boolean isPersonalInfoPublicAllowed() {
		return personalInfoPublicAllowed;
	}
	
	public String toString() {
		
		StringBuilder values = new StringBuilder();
		values.append("firstname = " + firstname);
		values.append("lastname = " + lastname);
		
		return values.toString();
	}


	public void setKey(SecretKey key) {
		this.key = key;
	}


	public SecretKey getKey() {
		return key;
	}
}
